<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
  
  <head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="manager/css/font.css">
    <link rel="stylesheet" href="manager/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="manager/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="manager/js/xadmin.js"></script>
    <style>
      img{
        width: 100px;
        height: 60px;
      }
       select{
         width: 200px;
         height: 38px;
       }
    </style>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  
  <body class="layui-anim layui-anim-up">
    <div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">演示</a>
        <a>
          <cite>导航元素</cite></a>
      </span>
      <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </div>
    <div class="x-body">
      <div class="layui-row">
        <form class="layui-col-md12 x-so" action="productList" method="post">
          <select id="ppc">
            <option th:selected="${ppc_id==0||ppc_id==null}" value="0">--请选择产品大类别--</option>
            <option th:each="productCategory:${productCategorys}" th:selected="${productCategory.pc_id==ppc_id}" th:text="${productCategory.pc_name}" th:value="${productCategory.pc_id}"></option>
          </select>
          <select id="pc"></select>
          <input type="hidden" name="pageNum" value="1" />
          <input type="hidden" name="pageSize" value="5" />
          <input type="text" name="p_name"  th:value="${p_name}" placeholder="请输入产品名" autocomplete="off" class="layui-input">
          <input type="hidden" name="ppc_id"  th:value="${ppc_id==null?0:ppc_id}">
          <input type="hidden" name="pc_id"  th:value="${pc_id==null?0:pc_id}" >
          <button class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
        </form>
      </div>
      <xblock>
        <button shiro:hasPermission="/delProduct" class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
        <button shiro:hasPermission="/productAdd" class="layui-btn" onclick="x_admin_show('添加产品','productAdd')"><i class="layui-icon"></i>添加</button>
        <span class="x-right" th:text="${'共有数据：'+pageInfo.pageCount+' 条'}" style="line-height:40px"></span>
      </xblock>
      <table class="layui-table">
        <thead>
          <tr>
            <th>
              <input type="checkbox" name="ckAll" >
            </th>
            <th>ID</th>
            <th>产品名</th>
            <th>产品图片</th>
            <th>产品所属类别</th>
            <th shiro:hasPermission="/updProductStatus" >状态</th>
            <th shiro:hasAnyPermissions="/updProductStatus,/productEdit,/delProduct" >操作</th>
        </thead>
        <tbody>
           <tr th:each="product:${pageInfo.list}">
             <td>
               <input type="checkbox" name="ck" th:value="${product.p_id}" >
             </td>
             <td th:text="${product.p_id}"></td>
             <td> <a th:text="${product.p_name}" th:onclick="x_admin_show('产品详情','productDetails?p_id=[[${product.p_id}]]')" href="javascript:void(0)"></a> </td>
             <td><img th:src="${product.p_image}"/></td>
             <td th:text="${product.pc_name}"></td>
             <td shiro:hasPermission="/updProductStatus" class="td-status">
               <span th:if="${product.p_status==1}" class="layui-btn layui-btn-normal layui-btn-mini">已启用</span>
               <span th:if="${product.p_status!=1}" class="layui-btn layui-btn-normal layui-btn-mini layui-btn-disabled">已停用</span>
             </td>
             <td shiro:hasAnyPermissions="/updProductStatus,/productEdit,/delProduct" class="td-manage">
               <a shiro:hasPermission="/updProductStatus" th:if="${product.p_status==1}" th:onclick="member_stop(this,'[[${product.p_id}]]')" href="javascript:;"  title="停用">
                 <i class="layui-icon">&#xe601; </i>
               </a>
               <a shiro:hasPermission="/updProductStatus" th:if="${product.p_status!=1}"  th:onclick="member_stop(this,'[[${product.p_id}]]')" href="javascript:;"  title="启用">
                 <i class="layui-icon">&#xe62f; </i>
               </a>
                 <a shiro:hasPermission="/productEdit" title="编辑"  th:onclick="x_admin_show('编辑','productEdit?p_id=[[${product.p_id}]]')" href="javascript:;">
                   <i class="layui-icon">&#xe642;</i>
                 </a>
                 <a shiro:hasPermission="/delProduct" title="删除" th:onclick="member_del(this,'[[${product.p_id}]]')" href="javascript:;">
                   <i class="layui-icon">&#xe640;</i>
                 </a>
             </td>
           </tr>
        </tbody>
      </table>
      <div class="page">
        <div>
          <a class="prev" th:if="${pageInfo.pageNum>1}"  href="javascript:void(0)" th:onclick="ss([[${pageInfo.pageNum-1}]],[[${pageInfo.pageSize}]])" >&lt;&lt;</a>
          <span class="prev" th:if="${pageInfo.pageNum<=1}" >&lt;&lt;</span>
          <span class="num" th:text="${pageInfo.pageNum+'/'+pageInfo.pages}"></span>
          <a class="next"  th:if="${pageInfo.pageNum < pageInfo.pages}" href="javascript:void(0)" th:onclick="ss([[${pageInfo.pageNum+1}]],[[${pageInfo.pageSize}]])">&gt;&gt;</a>
          <span class="prev" th:if="${pageInfo.pageNum>=pageInfo.pages}" >>></span>
        </div>
      </div>

    </div>
    <script>

      if('[[${ppc_id}]]'!='0'){
        $("#pc").html("")
        pc('[[${ppc_id}]]')
      }
      function pc(id){
        var pc_id = $("input[name='pc_id']").val();
        $.ajax({
          url:"productMin",
          data:{"ppc_id":id},
          method:"post",
          success:function (res) {
            if (res.length>0){
              $("#pc").append("<option "+(pc_id==0?'selected':'')+" value='0'>--请选择产品小类别--</option>")
              for (var i = 0; i < res.length; i++){
                $("#pc").append("<option "+(pc_id==res[i].pc_id?'selected':'')+" value='"+res[i].pc_id+"'>"+res[i].pc_name+"</option>")
              }
            }
          },
          dataType:"json"
        })
      }
      $("#ppc").change(function () {
          $("#pc").html("")
          $("input[name='pc_id']").val("0")
          $("input[name='ppc_id']").val($("#ppc").val())
          if($("#ppc").val()!='0'){
            pc($("#ppc").val())
          }
      })
      $("#pc").change(function () {
        $("input[name='pc_id']").val($("#pc").val())
      })
      layui.use('laydate', function(){
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
          elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
          elem: '#end' //指定元素
        });
      });

      function ss(pageNum,pageSize){
        $(":input[name='pageNum']").val(pageNum)
        $(":input[name='pageSize']").val(pageSize)
        $("form").submit();
      }

      $("input[name='ckAll']").click(function () {
        $("input[name='ck']").prop("checked",$("input[name='ckAll']").prop("checked"))
      })
      $("input[name='ck']").click(function () {
        var ss = $("input[name='ck']").length==$("input[name='ck']:checked").length;
        $("input[name='ckAll']").prop("checked",ss)
      })

      /*用户-停用*/
      function member_stop(obj,id){
        layer.confirm('确定要'+$(obj).attr('title')+'吗？',function(index){

          if($(obj).attr('title')=='启用'){
            //发异步把用户;状态进行更改
            $.ajax({
              url:"updProductStatus",
              data:{"p_id":id,"p_status":1},
              type:"post",
              success:function (result) {
                if(result.result=="0000"){
                  $(obj).attr('title','停用')
                  $(obj).find('i').html('&#xe601;');
                  $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('已启用');
                  layer.msg('已启用!',{icon: 6,time:1000});
                }else{
                  layer.msg("启用失败!",{icon:5,time:1000});
                }
              },
              error:function (result) {
                layer.msg('权限不够!',{icon:2,time:1000});
              },
              dataType:"json"
            })
          }else{
            $.ajax({
              url:"updProductStatus",
              data:{"p_id":id,"p_status":2},
              type:"post",
              success:function (result) {
                if(result.result=="0000"){
                  $(obj).attr('title','启用')
                  $(obj).find('i').html('&#xe62f;');
                  $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('已停用');
                  layer.msg('已停用!',{icon: 5,time:1000});
                }else{
                  layer.msg("停用失败!",{icon:5,time:1000});
                }
              },
              error:function (result) {
                layer.msg('权限不够!',{icon:2,time:1000});
              },
              dataType:"json"
            })
          }
        });
      }

      /*用户-删除*/
      function member_del(obj,id){
        layer.confirm('确认要删除吗？',function(index){
          //发异步删除数据
          $.ajax({
            url:"delProduct",
            data:{"ids":id},
            type:"post",
            success:function (result) {
              console.info(result)
              if(result.result=="0000"){
                $(obj).parents("tr").remove();
                layer.msg('已删除!',{icon:1,time:1000});
                $("form").submit();
              }else{
                layer.msg(result.resultMsg,{icon:2,time:1000});
              }
            },
            error:function (result) {
              layer.msg('权限不够!',{icon:2,time:1000});
            },
            dataType:"json"
          })
        });
      }


      function delAll (argument) {

        var data = tableCheck.getData();

        layer.confirm('确认要删除吗？'+data,function(index){
          //捉到所有被选中的，发异步进行删除
          cks = $("input[name='ck']:checked");
          var str = "";
          for (var i = 0; i < cks.length;i++){
            if(i == cks.length-1){
              str+=cks.eq(i).val();
            }else{
              str+=cks.eq(i).val()+",";
            }
          }
          $.ajax({
            url:"delProduct",
            data:{"ids":str},
            type:"post",
            success:function (result) {
              if(result.result=="0000"){
                layer.msg('删除成功', {icon: 1});
                $("form").submit();
              }else{
                layer.msg(result.resultMsg,{icon:1,time:1000});
              }
            },
            dataType:"json"
          })
        });
      }
    </script>
  </body>

</html>